package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.FormatMessage;
import yxy.game.pm2.module.battle.FighterModule;

import java.util.ArrayList;
import java.util.HashMap;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroBattleFormat
 * @description 心仪武将武将试炼布阵（提供的武将信息）
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroBattleFormat, classtype = ActiveMessage.ActBelovedHeroBattleFormatRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroBattleFormat extends Command<ActiveMessage.ActBelovedHeroBattleFormatRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroBattleFormatRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroBattleFormatResponse.Builder response = ActiveMessage.ActBelovedHeroBattleFormatResponse.newBuilder();

        try {

            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if(am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if(!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            //todo 上阵的武将
            Format format = am.getFormat();
            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();

            if (format != null) {
                FormatMessage.FormatArrary.Builder builder = FormatMessage.FormatArrary.newBuilder();
                builder.setNB(format.getNb());
                builder.addAllHeroJson(heroHandle.getAllHeroJsonArrary(format));
                response.setFormatArrary(builder);
            }

            //todo 可提供的武将
            HashMap<String, FighterModule> fighterModuleList = heroHandle.getFighterModuleList();
            ArrayList<FormatMessage.HeroJson> selectHeroJsonArrary = heroHandle.getSelectHeroJsonArrary(fighterModuleList);
            response.addAllHerojson(selectHeroJsonArrary);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }



    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroBattleFormatResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
